package com.controller.lib

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.controller.lib.download.OKDownloadHelper
import com.controller.lib.net.Api
import com.controller.lib.net.RetrofitClient
import timber.log.Timber
import timber.log.Timber.DebugTree

/**
 * SDK入口类
 */
interface OnCustomDirectiveListener {
    /**
     * 设备关机
     * */
    fun onCustomDirective(command: String)
    /**
     * 设备关机
     * */
    fun onShutDownDevice(command: String)
    /**
     * 设备重启
     * */
    fun onRestartDevice(command: String)
}

/**
 * SDK入口类
 */
interface OnDownLoadCompleteListener {
    /**
     * 图片下载完成
     * */
    fun onPicture(pictureUri: String)
    /**
     * 视频下载完成
     * */
    fun onVideo(filePath: String)
    /**
     * 文件下载完成
     * */
    fun onFile(filePath: String)
}

object DmSdk {

    private var sn: String? = null
    private var macAddress: String? = null
    private var isInitialized = false
    private lateinit var appContext: Context
    private var customDirectiveListener: OnCustomDirectiveListener? = null
    private var onDownLoadCompleteListener: OnDownLoadCompleteListener? = null
    /**
     * 初始化SDK
     * @param context Application context
     * @param sn 设备序列号. 如果为null, SDK将尝试自动获取.
     * @param macAddress 设备MAC地址. 如果为null, SDK将尝试自动获取.
     */
    fun init(context: Context, macAddress: String? = null) {
        if (isInitialized) {
            return
        }
        this.appContext = context.applicationContext
        this.sn = sn
        this.macAddress = macAddress
        isInitialized = true
        OKDownloadHelper.getInstance().initDownloader(context)
        RetrofitClient.init(Api.BASE_URL, true)
        //if (BuildConfig.DEBUG) {
        Timber.plant(DebugTree())
        // 启动核心服务
        startCoreService()
    }

    private fun startCoreService() {
        val intent = Intent(appContext, BootRequestService::class.java)
        appContext.startService(intent)
    }

    internal fun getSn(): String? {
        if(TextUtils.isEmpty(sn)) {
            sn = ControllerHelper.instance.getDeviceSN()
        }
        return sn
    }

    internal fun getMacAddress(): String? {
        if(TextUtils.isEmpty(macAddress)) {
            macAddress = ControllerHelper.instance.getMacAddress()
        }
        return macAddress
    }


    fun getContext(): Context = appContext

    fun setCustomDirectiveListener(listener: OnCustomDirectiveListener?) {
        customDirectiveListener = listener
    }

    internal fun notifyCustomDirective(command: String,commandType: String) {
        if(customDirectiveListener==null){
            Timber.e("onDownLoadCompleteListener is null, please set it before calling notifyCustomDirective")
            return
        }
        when (commandType) {
            Constants.SET_SHUT_DOWN -> customDirectiveListener?.onShutDownDevice(command)
            Constants.SET_RESTART -> customDirectiveListener?.onRestartDevice(command)
            Constants.CUSTOM_DIRECTIVE -> customDirectiveListener?.onCustomDirective(command)
            else -> Timber.w("notifyCustomDirective Unknown command type: $commandType")
        }
    }

    fun setOnDownLoadCompleteListener(listener:OnDownLoadCompleteListener?) {
        onDownLoadCompleteListener = listener
    }
    internal fun notifyDownLoadComplete(filePath: String,commandType: String) {
        if(onDownLoadCompleteListener==null){
            Timber.e("onDownLoadCompleteListener is null, please set it before calling notifyDownLoadComplete")
            return
        }
        when (commandType) {
            Constants.SET_SYS_BACKGROUND -> onDownLoadCompleteListener?.onPicture(filePath)
            Constants.SET_DISTRIBUTE_VIDEOS -> onDownLoadCompleteListener?.onVideo(filePath)
            Constants.SET_DOWNLOAD_FILE -> onDownLoadCompleteListener?.onFile(filePath)
            else -> Timber.w("notifyDownLoadComplete Unknown command type: $commandType")
        }
    }
} 